﻿using BonjourQuotation.ObjectModel.AccountData;
using BonjourQuotation.ObjectModel.SeasonData;
using BonjourQuotation.UI.Models.AccountModel;
using BonjourQuotation.UI.Models.SeasonModel;
using BonjourQuotation.UI.Models.PartialModel;
using BonjourQuotation.UI.Models.ReportModel;
using BonjourQuotation.UI.ServiceManagers;
using iTextSharp.text;
using iTextSharp.text.pdf;
using Microsoft.Reporting.WebForms;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using BonjourQuotation.UI.Models.Shared;


namespace BonjourQuotation.UI.Controllers
{
    public class SeasonController : BaseController
    {
        SeasonManager SeasonManager;
        [Authorize]
        [HttpGet]
        public ActionResult ManageSeasons()
        {
            SeasonManager = new SeasonManager();
            var ManageSeasonsData = SeasonManager.LoadSeasons(1);
            ManageSeasonsGridViewModel ManageSeasonsView = new ManageSeasonsGridViewModel();
            ManageSeasonsView.CurrentSearch = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var current in ManageSeasonsData.IsCurrent)
                ManageSeasonsView.CurrentSearch.Add(new System.Web.WebPages.Html.SelectListItem() { Text = current.Value, Value = current.Key.ToString() });
            this.SetPermission(ManageSeasonsView, AllPermVModel);
            return View("ManageSeasons", ManageSeasonsView);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult LoadSeasonsGrid(ManageSeasonsGridViewModel model)
        {
            SeasonManager = new SeasonManager();
            ManageSeasonsGridBData ManageSeasonsData = new ManageSeasonsGridBData();
            if (model.IsSearch)
            {
                ManageSeasonsGridBData SearchSeasonsData = new ManageSeasonsGridBData();
                SearchSeasonsData = InitSearchSeason(model);
                ManageSeasonsData = SeasonManager.LoadSearchedSeasons(SearchSeasonsData);
            }
            else
                ManageSeasonsData = SeasonManager.LoadSeasons(model.PageNum);
            var ManageSeasonsView = InitManageSeasonsVModel(ManageSeasonsData, model.PageNum);
            this.SetPermission(ManageSeasonsView, AllPermVModel);
            return PartialView("LoadSeasonsGrid", ManageSeasonsView);
        }
        [Authorize]
        [HttpGet]
        public PartialViewResult AddNewSeason()
        {
            ManageSeasonsGridRowViewModel SeasonDetData = new ManageSeasonsGridRowViewModel();
            return PartialView("AddSeasonGrid", SeasonDetData);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult SaveSeasonDetailData(ManageSeasonsGridViewModel model)
        {
            SeasonManager = new SeasonManager();
            var SeasonData = InitManageSeasonsGridBData(model);
            var EditError = SeasonManager.SaveSeasonDetailsData(SeasonData);
            MessageBoxViewModel MessBoxforEdit = new MessageBoxViewModel()
            {
                Message = EditError.Message != null ? "Error - " + EditError.Message : "",
            };
            return PartialView("MessageBox", MessBoxforEdit);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult AddNewSeason(ManageSeasonsGridRowViewModel model)
        {
            SeasonManager = new SeasonManager();
            var SeasonData = InitSeasonDetailsBData(model);
            var CreateError = SeasonManager.CreateSeason(SeasonData);
            MessageBoxViewModel MessBoxforCreate = new MessageBoxViewModel()
            {
                Message = CreateError.Message != null ? "Error - " + CreateError.Message : "",
            };
            return PartialView("MessageBox", MessBoxforCreate);
        }
        [Authorize]
        public PartialViewResult AutoComplete(string PropName, string keyword)
        {
            QuotationManager QuotaManager = new QuotationManager();
            var data = QuotaManager.FindKeywordInEntity(PropName, keyword);
            AutoCompleteViewModel AutoCompVMode = new AutoCompleteViewModel() { Items = data };
            return PartialView("QuotaAutoComplete", AutoCompVMode);
        }
        #region private methods
        private ManageSeasonsGridViewModel InitManageSeasonsVModel(ManageSeasonsGridBData ManageSeasonsData, int PageNum)
        {
            ManageSeasonsGridViewModel ManSeasonsVModel = new ManageSeasonsGridViewModel();
            ManSeasonsVModel.Rows = new List<ManageSeasonsGridRowViewModel>();
            foreach (var Season in ManageSeasonsData.Rows)
                ManSeasonsVModel.Rows.Add(new ManageSeasonsGridRowViewModel
                {
                    SeasonID = Season.SeasonID,
                    SeasonCode = Season.SeasonCode,
                    SeasonDescription = Season.SeasonDescription,
                    StartDate = Season.StartDate != null ? Convert.ToDateTime(Season.StartDate).ToString("dd/MMM/yy") : "",
                    FinishDate = Season.FinishDate != null ? Convert.ToDateTime(Season.FinishDate).ToString("dd/MMM/yy") : "",
                    IsCurrent = Season.IsCurrent != null ? Convert.ToBoolean(Season.IsCurrent) : false,
                });
            ManSeasonsVModel.CurrentSearch = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var current in ManageSeasonsData.IsCurrent)
                ManSeasonsVModel.CurrentSearch.Add(new System.Web.WebPages.Html.SelectListItem() { Text = current.Value, Value = current.Key.ToString() });
            if (ManSeasonsVModel.Rows.Count == ManageSeasonsGridBData.PageCount * PageNum)
                ManSeasonsVModel.IsCanView = true;
            else
                ManSeasonsVModel.IsCanView = false;
            return ManSeasonsVModel;
        }
      
        private ManageSeasonsGridBData InitSearchSeason(ManageSeasonsGridViewModel Data)
        {
            ManageSeasonsGridBData SrchData = new ManageSeasonsGridBData
            {
                SeasonCode = Data.SeasonCodeText,
                SeasonDescription = Data.SeasonDescriptionText,
                PageNum = Data.PageNum,
                SelectedIsCurrent = Data.IsCurrentSearch,
            };
            return SrchData;
        }
        private ManageSeasonsGridBData InitManageSeasonsGridBData(ManageSeasonsGridViewModel data)
        {
            ManageSeasonsGridBData ManSeasonsBData = new ManageSeasonsGridBData();
            ManSeasonsBData.Rows = new List<ManageSeasonsGridRowBData>();
            foreach (var Season in data.Rows)
                ManSeasonsBData.Rows.Add(new ManageSeasonsGridRowBData
                {
                    SeasonID = Season.SeasonID,
                    SeasonCode = Season.SeasonCode != null ? Season.SeasonCode : "-",
                    SeasonDescription = Season.SeasonDescription,
                    StartDate = Season.StartDate != null ? Convert.ToDateTime(Season.StartDate) : (DateTime?)null,
                    FinishDate = Season.FinishDate != null ? Convert.ToDateTime(Season.FinishDate) : (DateTime?)null,
                    IsCurrent = Season.IsCurrent,
                });
            return ManSeasonsBData;
        }
        private ManageSeasonsGridRowBData InitSeasonDetailsBData(ManageSeasonsGridRowViewModel data)
        {
            ManageSeasonsGridRowBData SeasonBData = new ManageSeasonsGridRowBData
            {
                SeasonID = 0,
                SeasonCode = data.SeasonCode != null ? data.SeasonCode : "-",
                SeasonDescription = data.SeasonDescription,
                StartDate = data.StartDate != null ? Convert.ToDateTime(data.StartDate):(DateTime?)null,
                FinishDate = data.FinishDate != null ? Convert.ToDateTime(data.FinishDate) : (DateTime?)null,
                IsCurrent = data.IsCurrent,
            };
            return SeasonBData;
        }
        #endregion
    }
}